package com.xrkj.b2c.service.impl.bus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xrkj.b2c.dao.bus.BusShopTypeMapper;
import com.xrkj.b2c.model.bus.BusShopType;
import com.xrkj.b2c.service.bus.BusShopTypeService;
import com.xrkj.b2c.view.ZTreeView;

/**
 * 
 * <B>系统名称：</B><BR>
 * <B>模块名称：</B><BR>
 * <B>中文类名：</B>商品类型服务类<BR>
 * <B>概要说明：</B><BR>
 * 
 * @author 赵子靖
 * @since 2018年07月15日
 */
@Service
public class BusShopTypeServiceImpl implements BusShopTypeService {
    @Autowired
    private BusShopTypeMapper shopTypeDao;

    @Override
    public int deleteShopType(String id) {
        return shopTypeDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insertShopType(BusShopType shopType) {
        return shopTypeDao.insertSelective(shopType);
    }

    @Override
    public int updateShopType(BusShopType shopType) {
        return shopTypeDao.updateByPrimaryKeySelective(shopType);
    }

    @Override
    public BusShopType selectByPrimaryKey(String id) {
        return shopTypeDao.selectByPrimaryKey(id);
    }

    @Override
    public Integer findShopTypeTotal(Map<String, Object> paramter) {
        return shopTypeDao.findShopTypeTotal(paramter);
    }

    @Override
    public List<BusShopType> findShopTypeList(Map<String, Object> paramter) {
        return shopTypeDao.findShopTypeList(paramter);
    }
    
    /**
     * 
     * <B>方法名称：</B>获取商品类别树<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年12月7日    
     * @return
     */
    public List<ZTreeView> getShopTypesTree(){
        //获取所有商品类别
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("stStatus", BusShopType.STATUS_1);
        List<ZTreeView> trees = new ArrayList<ZTreeView>();
        List<BusShopType> shopTypes = shopTypeDao.findShopTypeList(paramter);
        if(shopTypes!=null && shopTypes.size()>0){
            addChildShopType(shopTypes, trees, "root");
        }
        return trees;
    }
    
    /**
     * 
     * <B>方法名称：</B>递归装配商品类型<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年12月8日 	
     * @param shopTypes
     * @param tree
     * @param parendId
     */
    public void addChildShopType(List<BusShopType> shopTypes, List<ZTreeView> trees, String parendId) {
        boolean hasChild = false;
        for (BusShopType shopType : shopTypes) {
            if (shopType.getStParentId().equals(parendId)) {
                hasChild = true;
                break;
            }
        }
        if (!hasChild) {
            return;
        }
        for (BusShopType shopType : shopTypes) {
            if (shopType.getStParentId().equals(parendId)) {
                ZTreeView node = new ZTreeView();
                node.setId(shopType.getStId());
                node.setName(shopType.getStName());
                node.setpId(shopType.getStParentId());
                node.setOpen(false);
                trees.add(node);
                addChildShopType(shopTypes, trees, shopType.getStId());
            }
        }
    }
}
